<?php 
namespace Home\Controller;
use Think\Controller;
class ListController extends HomeController
{

    public function index()
    {
        // v($_GET);
        $cid = $_GET['cid'];
        // v($cid);exit;

        $cate = M('category')->field("cname, concat(`path`,`id`,',') bpath")->where('id='.$cid)->select();
        $cname = $cate[0]['cname'];
        $bpath = $cate[0]['bpath'];
        // v($cate);
        $where['path'] = array('LIKE', "$bpath%");
        // v($where);exit;
        $row = M('category')->field('id')->where($where)->select();
        // v($row);
        $ids = '';
        foreach ($row as $val) {
            $ids .= $val['id'].',';
        }
        $ids .= $cid;
        // v($ids);

        $w['a.cate_id'] = array('IN', $ids);
        // v($w);
        $first = M('category')
                ->field('a.title, i.img, u.name,  a.id aid')
                ->table('hx_article a, hx_image i, hx_user u, hx_headimg h')
                ->where('u.id=a.user_id AND i.art_id=a.id AND a.status=1')
                ->where($w)
                ->order('a.id desc')
                ->limit(1)
                // ->fetchSql(true)
                ->select();
        // v($first);exit;
        $data = M('category')
                ->field('a.title, i.img, u.name, a.time, u.motto, a.zan, a.id aid, u.id uid, h.img_40')
                ->table('hx_article a, hx_image i, hx_user u, hx_headimg h')
                ->where('u.id=a.user_id AND h.id=u.headimg_id AND i.art_id=a.id AND a.status=1')
                ->where($w)
                ->order('a.id desc')
                // ->fetchSql(true)
                ->limit(1,2)
                ->select();
        // v($data);
        
        $hot = M('category')
                ->field('a.title, i.img, a.id aid')
                ->table('hx_article a, hx_image i, hx_user u')
                ->where('u.id=a.user_id AND i.art_id=a.id AND a.status=1')
                ->where($w)
                ->order('a.read_times desc')
                ->limit(3)
                ->select();

        $comment = M('comment')
                ->field('c.c_content, u.name, a.title, a.id aid, u.id uid, h.img_40')
                ->table('hx_user u, hx_article a, hx_comment c, hx_headimg h')
                ->where('u.id=c.user_id AND h.id=u.headimg_id AND a.id=c.art_id AND a.id=7 AND c.user_id IN'.M('user')->field('id')->buildSql())
                // ->fetchSql(true)
                ->order('c.id desc')
                ->limit(1)
                ->select();
        // v($comment);exit;

        $this->assign('cname', $cname);
        $this->assign('first', $first);
        $this->assign('data', $data);
        $this->assign('hot', $hot);
        $this->assign('comment', $comment);
        $this->display('List/index');
    }

    //ajax加载二级页
    public function listAjax()
    {
        $cid = $_POST['cid'];
        $cate = M('category')->field("cname, concat(`path`,`id`,',') bpath")->where('id='.$cid)->select();
        $cname = $cate[0]['cname'];
        $bpath = $cate[0]['bpath'];
        // v($cate);
        $where['path'] = array('LIKE', "$bpath%");
        // v($where);
        $row = M('category')->field('id')->where($where)->select();
        $ids = '';
        foreach ($row as $val) {
            $ids .= $val['id'].',';
        }
        $ids .= $cid;
        $w['a.cate_id'] = array('IN', $ids);

        $n = $_POST['n'];
        $limit = (1+2*$n).",2";//后面两个1是显示1条

        $list = M('category')
                ->field('a.title, i.img, u.name, a.time, a.create_time,u.motto, a.zan, a.id aid, u.id uid, h.img_40')
                ->table('hx_article a, hx_image i, hx_user u, hx_headimg h')
                ->where('u.id=a.user_id AND h.id=u.headimg_id AND i.art_id=a.id AND a.status=1')
                ->where($w)
                ->order('a.id desc')
                ->limit($limit)
                // ->fetchSql(true)
                ->select();
        if (empty($list)) {
            $list = null;
        }
        $m = 0;
        foreach ($list as $key => $val) {
            $list[$m]['time'] = date('Y-m-d H:i:s', $val['time']);
            $m++;
        }
        $this->ajaxReturn($list);
    }
}



